package tmc.logic.actionListeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.swing.JFileChooser;

import org.apache.log4j.Logger;
import org.geotools.data.CachingFeatureSource;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.styling.SLDParser;
import org.geotools.styling.Style;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.swing.styling.JSimpleStyleDialog;
import org.opengis.feature.simple.SimpleFeatureType;

import tmc.gui.LayerControlPanel;
import tmc.gui.TMCFrame;
import tmc.main.TMC;

public class OpenFileActionListener implements ActionListener {

	private static Logger LOGGER = Logger
			.getLogger(OpenFileActionListener.class);

	final JFileChooser fc = new JFileChooser();
	TMCFrame parent;

	public OpenFileActionListener(TMCFrame frame) {
		parent = frame;
	}

	public void actionPerformed(ActionEvent event) {
		LOGGER.debug("Performing open file action.");
		FileDataStore store = null;
		SimpleFeatureSource featureSource = null;

		File file = JFileDataStoreChooser.showOpenFile("shp", null);
		if (file == null) {
			LOGGER.debug("No file selected.");
			return;
		}
		LOGGER.info("File selected : " + file.getAbsoluteFile());

		try {
			store = FileDataStoreFinder.getDataStore(file);
			featureSource = store.getFeatureSource();
			
		} catch (IOException ex) {
			LOGGER.error("Eception :" + ex.getMessage());
			LOGGER.error("Caused by : " + ex.getCause());
			ex.printStackTrace();
		}
		LOGGER.debug("Adding layer named: "	+ featureSource.getName().getLocalPart());
		LOGGER.debug("Creating user style");
		
		Style style = createStyle(file, featureSource);
		CachingFeatureSource cache = null;

		try {
			cache = new CachingFeatureSource(featureSource);
		} catch (IOException e) {
			LOGGER.error("Exception caching feature source :" + featureSource.getName());
			e.printStackTrace();
		}
		
		parent.addLayer(featureSource, style, file.getName());
	}

	private Style createStyle(File file, FeatureSource featureSource) {

		File styleFile = new File(file.getParent() +"\\" + file.getName().replace(".shp",".sld"));
		if (!styleFile.exists()) {
			styleFile = new File(file.getParent() +"\\" + file.getName().replace(".shp",".SLD"));
		}
		if (!styleFile.exists()) {
			
			LOGGER.debug("Creating user defined style");
			
			SimpleFeatureType schema = (SimpleFeatureType) featureSource.getSchema();
			return JSimpleStyleDialog.showDialog(null, schema);
			
		} else {
			
			SLDParser stylereader = null;
			
			try {
				
				stylereader = new SLDParser(TMC.getStyleFactory(), styleFile.toURI().toURL());
			} catch (Exception e) {
				LOGGER.error("Exception: " + e.getMessage());
				LOGGER.error("Cause :" + e.getCause());
				e.printStackTrace();
			}
			Style[] style = stylereader.readXML();
			return style[0];
		}
	}
}
